﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using KPIS.GERP.GBMS.IBLL;
using KPIS.GERP.GBMS.BLL;
using KPIS.GERP.GBMS.MODEL;
using KPIS.GERP.GBMS.APP_UI.Common;
//using KPIS.Utils;
using Telerik.Web.UI;

namespace KPIS.GERP.GBMS.APP_UI
{
    public partial class BGM_Mas_Option : System.Web.UI.Page
    {
        private IOptionBLL optCtrl = new OptionBLL();
        private IOptionTypeBLL optTypeCtrl = new OptionTypeBLL();
        private int optType;
        private RadTreeView tParent;
        private CommandControl cmdFac = new CommandControl();
        private Main main = new Main();

        protected void Page_Load(object sender, EventArgs e)
        {
            InitializeComponent();
            IList<OptionTypeInfo> optTypeList = optTypeCtrl.Search(optType);

            if (!IsPostBack)
            {
                SetDisplayControl(optTypeList[0]);
                LoadDataGrid(0, "", "", "", 0, optType);
            }
        }
        #region
        /// <summary>
        /// 
        /// </summary>
        ///Testtcfctct <param name="optTypeList"></param>
        private void SetDisplayControl(OptionTypeInfo optTypeList)
        {
            lblHeaderTee.Text = optTypeList.LabelName;
            lblHeader.Text = optTypeList.Name;
            lbConfigName.Text = optTypeList.LabelName;
            lbConfigParent.Text = optTypeList.LabelName;
            Grid.Columns[3].HeaderText = optTypeList.LabelName;
            lbConfigParent.Visible = (optTypeList.HasChild == 1);
            lbParent.Visible = (optTypeList.HasChild == 1);
            cbParent.Visible = (optTypeList.HasChild == 1);
        }

        private void OnNodeClick(object sender, RadTreeNodeEventArgs e)
        {
            //Grid.DataSource = optCtrl.Search(0, "", "", "", Convert.ToInt32(e.Node.Value), optType);
            //Grid.DataBind();
            hdOptParentSeq.Text = e.Node.Value;
            LoadDataGrid(0, "", "", "", Convert.ToInt32(e.Node.Value), optType);
            cmdFac.GridCommand(panelList, panelGrid, CmdCtrl);
        }

        private void LoadTree()
        {
            if(tView == null)
                InitializeComponent();
            tView.LoadTreeView();
            tParent.DataSource = optCtrl.TreeView(optType);
            tParent.DataBind();
            cbParent.Items[0].Value = "root";
            cbParent.Text = "";
        }

        private void LoadDataGrid(int seq, string code, string name, string desc, int parent, int type)
        {
            if (parent == 0)
            {
                Grid.DataSource = optCtrl.Search(seq, type, null);
                LoadTree();
            }
            else
            {
                Grid.DataSource = optCtrl.Search(seq, code, name, desc, parent, type, null);
            }
            Grid.DataBind();
        }

       private void InitializeComponent()
        {
            optType = Convert.ToInt32(Page.Request["optType"].ToString());
            tView.OptionType = optType;
            tParent = ((RadTreeView)cbParent.Items[0].FindControl("tParent"));

            CmdCtrl.Inserted += new EventHandler(OnInsert);
            CmdCtrl.Updated += new EventHandler(OnUpdate);
            CmdCtrl.Deleted += new EventHandler(OnDelete);

            tView.TreeNodeClick += new RadTreeViewEventHandler(OnNodeClick);
        }
        #endregion

       private void OnInsert(object sender, EventArgs e)
       {

           bool isDuplicated = optCtrl.CheckDuplicateCode(txtCode.Text);

           if (isDuplicated == false)
           {
               string str = txtCode.Text.Trim();
               Double num;
               bool isNum = Double.TryParse(str, out num);
               if (isNum == false)
               {
                   main.MessageAlert(Page, "กรอกเป็นตัวเลขเท่านั้น");
               }
               else
               {
                   int parent = (hdOptSeq.Text.Length > 0) ? Convert.ToInt32(hdOptSeq.Text) : 0;

                   optCtrl.Insert(txtCode.Text, txtName.Text, txtDescr.Text, parent, optType, null);
                   LoadDataGrid(0, "", "", "", 0, optType);
                   CmdCtrl.RefreshGrid();
               }
           }
           else
           {
               main.MessageAlert(Page, "รหัสซ้ำ");
           }
       }

        private void OnUpdate(object sender, EventArgs e)
        {
            bool isDuplicated = optCtrl.CheckDuplicateCode(txtCode.Text.Trim(), hdSeq.Text);

            if (isDuplicated == false)
            {
                string str = txtCode.Text.Trim();
                Double num;
                bool isNum = Double.TryParse(str, out num);
                if(isNum == false)
                {
                    main.MessageAlert(Page, "กรอกเป็นตัวเลขเท่านั้น");
                }
                else
                {

            int parent = (hdOptSeq.Text.Length > 0) ? Convert.ToInt32(hdOptSeq.Text) : 0;

            optCtrl.Update(Convert.ToInt32(hdSeq.Text), txtCode.Text, txtName.Text, txtDescr.Text, parent, optType);
            LoadDataGrid(0, "", "", "", 0, optType);
            CmdCtrl.MapControl();

                }
            }
            else
            {
                main.MessageAlert(Page, "รหัสซ้ำ");
            }
        }

        private void OnDelete(object sender, EventArgs e)
        {

            int nRowSelected = 0;

            if (panelGrid.Visible)
            {
                foreach (GridDataItem data in Grid.MasterTableView.GetSelectedItems())
                {
                    optCtrl.Delete(Convert.ToInt32(data.OwnerTableView.DataKeyValues[data.ItemIndex]["Sequence"]));

                    nRowSelected++;
                }
            }
            else
            {
                optCtrl.Delete(Convert.ToInt32(hdSeq.Text));
            }

            LoadDataGrid(0, "", "", "", 0, optType);
            CmdCtrl.RefreshGrid();
        }

        protected void Grid_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "TargetClick")
            {
                IList<OptionInfo> optList = optCtrl.Search(Convert.ToInt32(((Label)e.Item.FindControl("targetSequence")).Text), optType, null);

                if (optList.Count > 0)
                {
                    hdSeq.Text = optList[0].Sequence.ToString();
                    txtCode.Text = optList[0].Code;
                    txtName.Text = optList[0].Name;
                    txtDescr.Text = optList[0].Description;

                    if (optList[0].Parent != null)
                    {
                        IList<OptionInfo> optParentList = optCtrl.Search(optList[0].Parent.Sequence, optType, null);

                        cbParent.SelectedValue = "root";
                        cbParent.SelectedItem.Text = optParentList[0].Name;
                        hdOptSeq.Text = optParentList[0].Sequence.ToString();

                        //tParent.SelectedNode.Value = optParentList[0].Sequence.ToString();
                    }


                    CmdCtrl.MapControl();
                }
            }
            else
            {
                int tmpParentSeq = hdOptParentSeq.Text.Length > 0 ? int.Parse(hdOptParentSeq.Text) : 0;
                LoadDataGrid(0, "", "", "", tmpParentSeq, optType);
            }
        }

        protected void RadManager1_AjaxRequest(object sender, Telerik.Web.UI.AjaxRequestEventArgs e)
        {
            hdOptSeq.Text = e.Argument;
        }
    }
}
